package cn.agiledata.bank.linkbank.impl.cib;

import java.util.Date;

import cn.agiledata.bank.account.service.ISequenceGenerator;
import cn.agiledata.bank.common.util.DateUtil;
import cn.agiledata.bank.linkbank.logic.OrderIdTransformer;

/**
 * 中国兴业银行订单编号转换器。提供支付系统交易编号与向兴业银行提交的订单编号的转换逻辑
 * 
 * @author 马贤波
 * 
 */
public final class CibOrderIdTransformer implements OrderIdTransformer {
	private ISequenceGenerator sequenceGenerator;

	public void setSequenceGenerator(ISequenceGenerator sequenceGenerator) {
		this.sequenceGenerator = sequenceGenerator;
	}

	public String orderId2TransId(String orderId) {
		if (orderId == null || orderId.length() != 12)
			return null;
		String yymmdd = orderId.substring(0, 6);
		Date date = DateUtil.string2Date(yymmdd, "yyMMdd");
		String sequenceNo = orderId.substring(orderId.length() - 6);
		return this.sequenceGenerator.getTransactionSequence(date, sequenceNo);
	}

	public String transId2OrderId(String transId) {
		if (transId == null)
			return null;
		return transId.substring(2, 8)
				+ transId.substring(transId.length() - 6);
	}

}
